﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

[ExecuteInEditMode]
public class CommonUVExporter : MonoBehaviour
{
   
    private class IndexInfo
    {
        public Vector4 uv;
        public string spriteName;
    }


    public string OutLuaPath = "";

    public string LuaFileName = "UVConfig";
    
    public Texture2D spriteAtlas;

    public string spriteSubName = "lc_public_ground";
    
    private float floatOffset = 0f;
    
    private List<IndexInfo> UVTable = new List<IndexInfo>();

    private Dictionary<string,Rect> uvRectDic = new Dictionary<string, Rect>();

    public void SpawnConfig()
    {

#if UNITY_EDITOR
        
        UVTable.Clear();
        uvRectDic.Clear();
        
        string texturePath = AssetDatabase.GetAssetPath(spriteAtlas.GetInstanceID());

        TextureImporter importer = (TextureImporter) AssetImporter.GetAtPath(texturePath);
 
        for(int i=0;i < importer.spritesheet.Length;i++)
        {
            Rect uv = new Rect();

            uv.x = importer.spritesheet[i].rect.x / spriteAtlas.width + floatOffset;
            uv.y = importer.spritesheet[i].rect.y / spriteAtlas.height + floatOffset;
            
            uv.width = importer.spritesheet[i].rect.width/spriteAtlas.width - floatOffset;
            uv.height = importer.spritesheet[i].rect.height/spriteAtlas.height - floatOffset;

            uvRectDic[importer.spritesheet[i].name] = uv;
            //change pixel coords to uvs.
        }


        for (int i = 1; i <= uvRectDic.Count; i++)
        {
            string spriteName = spriteSubName + "_" + i.ToString();

            Rect uvRect = uvRectDic[spriteName];
        
            // 左下角
            Vector2 leftBottom = new Vector2(uvRect.x, uvRect.y);
            Vector2 rightTop = new Vector2(uvRect.x + uvRect.width ,uvRect.y + uvRect.height);
            
            Vector4 v = new Vector4(leftBottom.x, leftBottom.y, rightTop.x, rightTop.y);

            v.w *= 0.1f;
            
            UVTable.Add(new IndexInfo()
            {
                uv = v,
                spriteName = spriteName
            });
            
        }
        
        
        string path = Application.dataPath + "/../Lua/" + OutLuaPath;

        string filePath = path + LuaFileName + ".lua";
        if (!Directory.Exists (path)) {
            Directory.CreateDirectory (path);
        }
        StringBuilder sb = new StringBuilder ();
        sb.AppendLine ("return {");


        for (int i = 0; i < UVTable.Count; i++)
        {
            
            Vector4 col = UVTable[i].uv;

            sb.Append(string.Format ("[{0}] = {{\n color = Color.New({1},{2},{3},{4}),\n spriteName = \"{5}\"\n}}\n,",i+1,col.x,col.y,col.z,col.w,UVTable[i].spriteName));
        }
        
        
        sb.AppendLine ("}");

        StreamWriter streamWriter = new StreamWriter (filePath);
        streamWriter.Write (sb.ToString ());
        streamWriter.Flush ();
        streamWriter.Close ();

        AssetDatabase.Refresh ();
        Debug.Log ("导出数据成功：" + filePath);

#endif
    }

}
